<?php
/**
 * blog控制器
 */

class Article extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->lists();
    }

    public function detail($id)
    {
        $id = intval( $id );
        if(!$id || $id < 0)
            return false;

        $article = $this->post_model->get($id);
        if(!$article)
            return false; //提示错误

        $article['title'] = title($article['title']);
        $article['content'] = content($article['content']);
        $article['tags'] = tags($article['tags']);

        $article['reviews'] = array();
        if($article['review_num'])
        {
            $article['reviews'] = $this->post_model->getReviews($id);
            foreach($article['reviews']['result'] as $key => $val)
            {
                $article['reviews']['result'][$key]['comments'] = array();
                if($val['comment_num'])
                {
                    $article['reviews']['result'][$key]['comments'] = $this->post_model->getComments($val['id']);
                }
            }
        }


        $this->addreview($id);

        $this->tpl->assign('article', $article);
        $this->tpl->display('article_detail.html');
        
    }

    public function tag($tag=false)
    {
        if(!$tag)
            return false;

        $result = $this->post_model->getArticlesByTag($tag);

        $this->tpl->assign('article_total', $result['total']);
        $this->tpl->assign('articles', $result['result']);
        $this->tpl->display('article_list.html');
    }

    public function lists()
    {
        $result = $this->post_model->getArticles();
        
        $this->tpl->assign('article_total', $result['total']);
        $this->tpl->assign('articles', $result['result']);
        $this->tpl->display('article_list.html');
    }

    public function add()
    {
        if(!$this->user_model->is_login() || !$this->user_model->is_active())
            header('location:/user/login');
        
        if($this->input->server('REQUEST_METHOD') == 'POST')
        {
            $post['title'] = $this->input->post('title');
            $post['content'] = $this->input->post('content');
            $post['tags'] = $this->tag_model->doTag($this->input->post('tags'));
            $post['author'] = $this->user_model->get_id();
            if( $this->post_model->add($post, 'article') )
            {
                //发布成功
                header('location:/article');
            }
            //提示错误
            
        }
        $this->tpl->display('article_add.html');
    }

    public function edit($id)
    {
        if(!$this->user_model->is_login() || !$this->user_model->is_active())
            header('location:/user/login');
        
        $id = intval( $id );
        if(!$id || $id < 1)
            return false;//提示错误

        $article = $this->post_model->get($id);
        if(!$article)
            return false; //提示错误

        if($this->user_model->get_id() != $article->author)
            return false;

        if( $this->input->server('REQUEST_METHOD') == 'POST' && ($id == $this->input->post('id')) )
        {
            $post['title'] = $this->input->post('title');
            $post['content'] = $this->input->post('content');
            $post['tags'] = $this->tag_model->doTag($this->input->post('tags'));
            $post['editor'] = $this->user_model->get_id();
            $post['active_time'] = date('Y-m-d H:i:s', time());
            if( !$this->post_model->update($post, $this->input->post('id')) )
            {
                header('location:/article/edit/'.$this->input->post('id'));
            }
            //header();跳转到详细页
            header('location:/article');
            exit;
        }

        $article = (array)$article;
        $this->tpl->assign('article', $article);
        $this->tpl->display('article_edit.html');      
        
    }

    public function del()
    {
        
    }

    public function addreview($pid)
    {

        if($this->input->server('REQUEST_METHOD') != 'POST')
            return false;
        
        $remember_me = false;
        $review_error = array();
        $uid = 0;
        if(!$this->user_model->is_login())
        {
            $userinfo['email'] = trim($this->input->post('email'));
            $userinfo['name'] = trim($this->input->post('name'));
            $remember_me = trim($this->input->post('remember_me'));

            $this->load->helper('email');
            if(empty($userinfo['email']) || !valid_email($userinfo['email']))
                $review_error['email'] = '请输入合法邮箱地址';

            if(empty($userinfo['name']))
                $review_error['name'] = '请输入对您的称呼';
            if(!count($review_error))
            {
                //添加用户
                $oUser = $this->user_model->find_by_email($userinfo['email']);
                $userinfo['passwd'] = '111111';
                if(!$oUser)
                {
                    $uid = $this->user_model->add($userinfo);
                }
                else
                {
                    $uid = $oUser->user_id;
                }


                if($remember_me != '')
                {
                    $remember_me = true;
                }
                else
                {
                    $remember_me = false;
                }

                if( !$this->user_model->login($userinfo['email'], $userinfo['passwd'], $remember_me) )
                {
                    $review_error['account'] = '请先登录帐号';
                }
            }
        }

        $post['pid'] = $this->input->post('pid');
        $post['author'] = $this->user_model->get_id() ?  $this->user_model->get_id() : $uid;
        $post['content'] = $this->input->post('content');
        if(strlen($post['content']) < 10)
        {
            $review_error['content'] = '内容不能少于10字符';
        }
        if(!count($review_error))
        {
            if( $this->post_model->add($post, 'review') )
            {
                //更新article active_time
                $this->post_model->update(array('active_time'=>date('Y-m-d H:i:s', time())), $post['pid']);

                header('location:/article/detail/'.$post['pid']);
            }
            //header('location:/article/detail/'.$_POST['pid']);
        }
        $this->tpl->assign('review_error', $review_error);
        $this->tpl->assign('review_post', $post);
        $this->tpl->assign('review_user', $userinfo);

    }
}